package com.yuan.rest.controller.admin;

import com.yuan.rest.entrity.SysMenu;
import com.yuan.rest.repository.SysMenuRepository;
import com.yuan.rest.util.Result;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @Auther: SiYuan
 * @Date: 2019/3/4 16:01
 * @Description:
 */

@Controller
@RequestMapping("menu")
public class MenuController {
    @Autowired
    SysMenuRepository sysMenuRepository ;

    @GetMapping("/list")
    public String roleList() {
        return "admin/menu_list";
    }

    /***
     *  获取用户列表  layUi 的表格数据
     * @param
     * @return
     */
    @GetMapping("/menus")
    @ResponseBody
    public Map<String, Object> getUserList() {
        Result result = new Result();
        List<SysMenu> all = sysMenuRepository.findAll();
        result.put("code", 0);
        result.put("msg", "查询成功");
        result.put("count", all.size());
        result.put("data", all);
        return result;
    }

    /**
     * 添加菜单
     * @param jsonObject
     * @return
     */
    @PostMapping("add")
    @ResponseBody
    public Result addUser(@RequestBody JSONObject jsonObject) {
        SysMenu sysMenu = (SysMenu) JSONObject.toBean(jsonObject, SysMenu.class);
        sysMenuRepository.save(sysMenu);
        return Result.ok();
    }

    /***
     *  删除菜单
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public Result Menu(@PathVariable long id) {
        sysMenuRepository.deleteById(id);
        return Result.ok();
    }

    /***
     * 根据角色id 名查询角色信息
     *
     */
    @GetMapping("{id}")
    @ResponseBody
    public Result getMenuInfo(@PathVariable long id){
        Optional<SysMenu> byId = sysMenuRepository.findById(id);
        return Result.ok().put("data",byId);
    }



}
